package com.wad.manager.config;

import com.wad.manager.interceptor.LoginAuthInterceptor;
import com.wad.manager.properties.UserProerties;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author wad
 * @date 2023/11/29 16:09
 * @project e-commerce
 **/
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {


    @Resource
    private LoginAuthInterceptor loginAuthInterceptor;
    @Resource
    private UserProerties userProerties;

    //跨域配置
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") //配置路径
                .allowCredentials(true) //是否允许在跨域的情况下传递cookie
                .allowedOriginPatterns("*") //允许请求来源的域规则
                .allowedMethods("*") //允许所有请求方法
                .allowedHeaders("*"); //允许所有请求头
    }


    //注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginAuthInterceptor)
                .excludePathPatterns(userProerties.getNoAuthUrls())
                .addPathPatterns("/**");
    }
}
